package org.csu.molediary.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Commentinfo;
import org.csu.molediary.api.entity.Friend;
import org.csu.molediary.api.persistence.AccountMapper;
import org.csu.molediary.api.persistence.CommentinfoMapper;
import org.csu.molediary.api.service.CommentInfoService;
import org.csu.molediary.api.vo.CommentInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service("CommentInfoService")
public class CommentInfoServiceImpl implements CommentInfoService {
    @Autowired
    private CommentinfoMapper commentinfoMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    //保存发出评论
    public CommonResponse<Commentinfo> saveCommentinfo(Commentinfo commentinfo) {

        commentinfoMapper.insert(commentinfo);
        QueryWrapper<Commentinfo> commentinfoQueryWrapper=new QueryWrapper<>();
        commentinfoQueryWrapper.eq("comcontent",commentinfo.getComment());
        Commentinfo commentinfo1=commentinfoMapper.selectOne(commentinfoQueryWrapper);

        return CommonResponse.createForSuccess(commentinfo1);
    }

    @Override
    //获得某用户被评论信息
    public CommonResponse<List<Commentinfo>>queryCommentinfoByCommented(String Commented) {

        QueryWrapper<Commentinfo> commentinfoQueryWrapper=new QueryWrapper<>();
        commentinfoQueryWrapper.eq("commented",Commented);
        List<Commentinfo> commentinfoList=commentinfoMapper.selectList(commentinfoQueryWrapper);
        return CommonResponse.createForSuccess(commentinfoList);
    }

    @Override
    public CommonResponse<List<CommentInfoVO>> queryCommentInfoByDiaryId(int diaryid) {
        QueryWrapper<Commentinfo> commentinfoQueryWrapper=new QueryWrapper<>();
        commentinfoQueryWrapper.eq("diaryid",diaryid);
        List<Commentinfo> commentinfo=commentinfoMapper.selectList(commentinfoQueryWrapper);

        List<CommentInfoVO> commentInfoVOS=new ArrayList<>();

        for(int i=0;i<commentinfo.size();i++){
            QueryWrapper<Account> accountQueryWrapper=new QueryWrapper<>();
            accountQueryWrapper.eq("id",commentinfo.get(i).getComment());

            CommentInfoVO commentInfoVO=new CommentInfoVO();

            commentInfoVO.setCommentinfo(commentinfo.get(i));
            commentInfoVO.setHead(accountMapper.selectOne(accountQueryWrapper).getHead());
            commentInfoVO.setName(accountMapper.selectOne(accountQueryWrapper).getName());
            commentInfoVOS.add(commentInfoVO);
        }
        if(commentInfoVOS.size()!=0){
            return CommonResponse.createForSuccess(commentInfoVOS);
        }
        else
            return CommonResponse.createForSuccessMessage("没有评论");
    }


}
